//
//  ViewController.m
//  UserProfie
//
//  Created by Lorwy on 15/3/27.
//  Copyright (c) 2015年 Lorwy. All rights reserved.
//

#import "ViewController.h"
#import "UIImage+StackClearness.h"

#define HeaderHeight 200.0f
#define kBaseBlurr (200)

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIButton *userPhoto;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) CGFloat lastOffset;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    source = [UIImage imageNamed:@"sampleImage.jpeg"];
    self.imageView = [[UIImageView alloc] initWithImage:source];
    self.imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, HeaderHeight);
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.clipsToBounds = YES;
    
    userPhoto = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
    userPhoto.center = CGPointMake(CGRectGetMidX(self.imageView.bounds), CGRectGetMidY(self.imageView.bounds));
    [userPhoto setImage:[UIImage imageNamed:@"sampleImage.jpeg"] forState:UIControlStateNormal];
    userPhoto.clipsToBounds = YES;
//    userPhoto.backgroundColor = [UIColor redColor];
    [userPhoto.layer setCornerRadius:75];
    [self.imageView addSubview:userPhoto];
    
    
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HeaderHeight)];
    tableHeaderView.backgroundColor = [UIColor purpleColor];
    [tableHeaderView addSubview:self.imageView];
    self.tableView.tableHeaderView = tableHeaderView;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 4;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        UITableViewCell  *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        return cell;
    }
    UITableViewCell  *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:@"icon.jpeg"];
    cell.textLabel.text=@"我的车库";
    cell.detailTextLabel.text = @"新消息";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


#pragma mark - UIScrollView delegate

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"contentOffset= %f",scrollView.contentOffset.y);
    
    if (scrollView.contentOffset.y < 0) {
        self.imageView.image=[source stackClearness:(-scrollView.contentOffset.y/12)];
        CGRect imgRect = self.imageView.frame;
        imgRect.origin.y = scrollView.contentOffset.y;
        imgRect.size.height = HeaderHeight + fabsf(scrollView.contentOffset.y);
        self.imageView.frame = imgRect;
        userPhoto.center = CGPointMake(CGRectGetMidX(self.imageView.bounds), CGRectGetMidY(self.imageView.bounds));
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
